var message = new Message();
function validate(form) {
	var username = form.username.value;
	var passwd = form.passwd.value;
	var error = [];
	if (!checkLength(username, 4, 20)) {
		error.push(message.data.LOST_USERNAME);
	} else if (!checkLength(passwd, 6, 10)) {
		error.push(message.data.LOST_PASSWD);
	}
	if (error.length > 0) {
		reportError(error);
		return false;
	}
}
/**
 * validate password
 */
function validateChangePasswd(form) {
	var currentpasswd = form.currentpasswd.value;
	var newpasswd = form.newpasswd.value;
	var checkpasswd = form.checkpasswd.value;
	var error = [];
	if (!checkLength(currentpasswd, 6, 10)) {
		error.push(message.data.LOST_CURRPASSWD);
	} else if (!checkLength(newpasswd, 6, 10)) {
		error.push(message.data.LOST_NEWPASSWD);
	} else if (!checkLength(checkpasswd, 6, 10)) {
		error.push(message.data.LOST_CONFIRMPASSWD);
	}
	if (error.length > 0) {
		reportError(error);
		return false;
	}
}

/**
 * check submit to add new item when click to 「新規入力」button
 */
function validateSubmit(form) {
	var capacity = form.productcapacity.value;
	var quantity = form.productquantity.value;
	var weight = form.productweight.value;
	// need validate value of quantity and weight;
	var error = [];
	if (capacity == -1)
		error.push(message.data.LOST_CAPACITY);
	else {
		if (capacity.indexOf('R') == -1) {
			if (!is_int(quantity))
				error.push(message.data.QUANTITY + message.data.W_0024);
			else if (!checkZeroAndMinus(quantity))
				error.push(message.data.QUANTITY + message.data.W_0025);
		}
		if (!is_int(weight))
			error.push(message.data.WEIGHT + message.data.W_0024);
		else if (!checkZeroAndMinus(weight))
			error.push(message.data.WEIGHT + message.data.W_0025);
	}
	if (error.length > 0) {
		reportError(error);
		return false;
	}
}
/**
 * check search
 */
function validateSearch(form) {
	var year = form.sup_year.value;
	var month = form.sup_month.value;
	var text_input = form.productcode.value;
	var error = [];
	if (year == "0" && month != "0")
		error.push(message.data.ERROR_SElECT_DATE);
	if (error.length > 0) {
		reportError(error);
		return false;
	}
}
function validateNumber(object) {
	var error = [];
	if (!is_int(object))
		error.push(message.data.W_0024);
	else if (!checkZeroAndMinus(object))
		error.push(message.data.W_0025);
	if (error.length > 0) {
		reportError(error);
		return false;
	}
}
function is_int(value) {
	if ((parseFloat(value) == parseInt(value)) && !isNaN(value))
		return true;
	else
		return false;
}
function checkZeroAndMinus(value) {
	if (value <= 0)
		return false;
	else
		return true;
}

function checkLength(text, min, max) {
	min = min || 1;
	max = max || 1000;
	if (text.length < min || text.length > max) {
		return false;
	}
	return true;
}

/**
 * when click to 「削除」button -> 確認ボックス表示
 */
function refLink() {
	var seqNo = document.getElementById("seqNo").value;
	new Messi('本当に削除したいですか？？？', {
		title : '削除確認',
		buttons : [ {
			id : 0,
			label : 'Yes',
			val : true
		}, {
			id : 1,
			label : 'No',
			val : false
		} ],
		callback : function(val) {
			if (val) {
				window.location.href = 'deleteYoyaku.action?seqNo=' + seqNo;
			}
		}
	});
	return false;
}

function reportError(errors) {
	$('.messi,.messi-modal').remove();
	var numError;
	var err = "▼エラー一覧 :" + "<br/>";
	for (var i = 0; i < errors.length; i++) {
		numError = i + 1;
		err += "  " + numError + " : " + errors[i] + "<br/>";
	}
	new Messi(err, {
		title : 'エラー発生！',
		titleClass : 'anim error',
		closeButton : true,
		modal : true,
		unload : true,
		buttons : [ {
			id : 0,
			label : 'クローズ',
			val : 'X'
		} ]
	});
	return false;
}
